package com.aiyige.page.login.presenter;

import android.app.Activity;

import com.aiyige.MyApp;
import com.aiyige.R;
import com.aiyige.base.BaseActivity;
import com.aiyige.page.login.callbacks.ISendVerifyCodeCallBack;
import com.aiyige.page.login.model.IRegisterModel;
import com.aiyige.page.login.model.impl.RegisterImplement;
import com.aiyige.page.login.view.IRegisterView;
import com.aiyige.utils.ToastX;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.vondear.rxtools.RxDataTool;

/**
 *
 */
public class RegisterPresenter {
    private IRegisterModel mModel;
    private IRegisterView mView;

    /**
     * 把Activity的需要用到的model通过ILoginView接口传递到Presenter来
     * @param mRegisterView
     */
    public RegisterPresenter(IRegisterView mRegisterView)
    {
        this.mView = mRegisterView;
        this.mModel = new RegisterImplement();
    }

    public void next(Activity activity)
    {
        if (!checkInput()) {
            return;
        }
        if (RxDataTool.isNullString(mView.getUserName())) {
            ToastX.show(MyApp.getInstance().getString(R.string.phone_can_not_empty));
            return;
        }
        if (mView.getVerifyCode().length() < 6) {
            ToastX.show(MyApp.getInstance().getString(R.string.verify_code_incomplete));
            return;
        }
        mModel.checkVerifyCode(mView.getUserName(), mView.getVerifyCode());
    }

    public void sendVerifyCode() {
        if (checkInput()) {
            mModel.sendVerifyCode(mView.getMyContext(),mView.getUserName(), new ISendVerifyCodeCallBack() {
                @Override
                public void success(String verifyCode) {
                    mView.setVerifyCode(verifyCode);
                }
            });
        }
    }

    private boolean checkInput() {
        boolean pass = false;
        if (!RxDataTool.isNullString(mView.getUserName())) {
            pass = true;
        } else {
            ToastX.show(MyApp.getAppContext().getString(R.string.phone_can_not_empty));
        }
        return pass;
    }

    public void loginWeChat(BaseActivity activity) {
        mModel.loginByOther(activity, SHARE_MEDIA.WEIXIN);
    }

    public void loginQQ(BaseActivity activity) {
        mModel.loginByOther(activity,SHARE_MEDIA.QQ);
    }

    public void loginWeibo(BaseActivity activity) {
        mModel.loginByOther(activity,SHARE_MEDIA.SINA);
    }

}
